package cn.lizemin.flowable.service;

import cn.lizemin.flowable.entity.User;
import cn.lizemin.flowable.entity.req.AskForLeaveReq;
import org.flowable.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * @author lzm
 * @Date 2024/6/30
 * @description
 */
@Service
public class AskForLeaveService {

    @Autowired
    RuntimeService runtimeService;

    @Transactional
    public void askForLeave(AskForLeaveReq askForLeaveReq) {
        HashMap<String, Object> vars = new HashMap<>();
        vars.put("approver", askForLeaveReq.getApprover());
        vars.put("days", askForLeaveReq.getDays());
        vars.put("reason", askForLeaveReq.getReason());
        vars.put("startTime", askForLeaveReq.getStartTime());
        vars.put("endTime", askForLeaveReq.getEndTime());

        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        vars.put("applicant", user.getUsername());
        runtimeService.startProcessInstanceById("ask_for_leave", vars);
    }
}
